package uk.co.q3c.deplan.client.util;

import java.util.Date;
import java.util.logging.Logger;

import uk.co.q3c.deplan.client.util.apache.StringUtils;

public class Q3DateUtil {
	protected static final Logger logger = Logger.getLogger(Q3DateUtil.class.getName());

	/**
	 * Returns a new date instance set to <code>date</code>, adjusted to the beginning of the day
	 * 
	 * @param date
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static Date startOfDay_date(Date date) {
		if (date == null) {
			return null;
		}
		Date d = new Date(0);
		d.setYear(date.getYear());
		d.setMonth(date.getMonth());
		d.setDate(date.getDate());
		d.setHours(9);
		return d;
	}

	@SuppressWarnings("deprecation")
	public static String dateToKey(Date date) {
		if (date == null) {
			return null;
		}
		StringBuffer buf = new StringBuffer();
		buf.append(Integer.toString(date.getYear() + 1900));
		String s = Integer.toString(date.getMonth() + 1);
		buf.append(StringUtils.leftPad(s, 2, "0"));
		s = Integer.toString(date.getDate());
		buf.append(StringUtils.leftPad(s, 2, "0"));
		return buf.toString();
	}

	@SuppressWarnings("deprecation")
	public static Date keyToDate(String dateKey) {
		if (dateKey == null) {
			return null;
		}
		String syear = dateKey.substring(0, 4);
		String smonth = dateKey.substring(4, 6);
		String sdate = dateKey.substring(6, 8);
		int year = Integer.parseInt(syear);
		int month = Integer.parseInt(smonth);
		int day = Integer.parseInt(sdate);
		Date d = new Date();
		d.setTime(0);
		logger.info(Long.toString(d.getTime()));
		d.setYear(year - 1900);
		logger.info(Long.toString(d.getTime()));
		d.setMonth(month - 1);
		logger.info(Long.toString(d.getTime()));
		d.setDate(day);
		logger.info(Long.toString(d.getTime()));
		d.setHours(0);
		logger.info(Long.toString(d.getTime()));
		d.setMinutes(0);
		logger.info(Long.toString(d.getTime()));
		d.setSeconds(0);
		logger.info(Long.toString(d.getTime()));
		return d;

	}

	/**
	 * Null safe version using standard {@link Date#before(Date)} method. Returns the earliest of the given dates. If either date is null
	 * the other date is returned. If both are null, null is returned
	 * 
	 * @see #latestOf
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static Date earliestOf(Date date1, Date date2) {
		if (date1 == null) {
			return date2;
		}
		if (date2 == null) {
			return date1;
		}
		if (date1.before(date2)) {
			return date1;
		}
		return date2;

	}

	/**
	 * Null safe version using standard {@link Date#after(Date)} method. Returns the latest of the given dates. If either date is null the
	 * other date is returned. If both are null, null is returned
	 * 
	 * @see #earliestOf
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static Date latestOf(Date date1, Date date2) {
		if (date1 == null) {
			return date2;
		}
		if (date2 == null) {
			return date1;
		}
		if (date1.after(date2)) {
			return date1;
		}
		return date2;

	}

	// private static final String pattern = "yyyyMMdd";
}
